package com.ylyq.ylxx.intercepter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

@Configuration
@Slf4j
public class WebCorsConfigFilter implements Filter {

    /*跨域请求配置*/
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        HttpServletResponse response1 = (HttpServletResponse) response;
        HttpServletRequest request1 = (HttpServletRequest) request;
        response1.setHeader("Access-Control-Allow-Origin", "*");
        response1.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, HEAD");
        response1.setHeader("Access-Control-Max-Age", "3600");
        response1.setHeader("Access-Control-Allow-Headers", "*");
        response1.setHeader("Access-Control-Allow-Credentials", "true");

        StringBuilder sb = new StringBuilder();
        sb.append("host：" + request1.getHeader("host") + "<br/>");
//        sb.append("host：" + request1.getRemoteHost() + "<br/>");
        sb.append("token：" + request1.getHeader("token") + "<br/>");
//        String readAsChars = ReadAsChars3(request1);//使用会报错
//        sb.append("parameterMap---->" + readAsChars);

        //这个会得到类似这样的结果http://localhost:8081/ylyq/
//        String basePath  = request1.getScheme() + "://" + request1.getServerName() + ":" + request1.getServerPort() + request1.getContextPath() + "/";
        log.debug("url:{},method:{},headers:{}", request1.getRequestURI(), request1.getMethod(), sb.toString());
//        log.debug("url:{},method:{},headers:{}", request1.getServerName(), request1.getMethod(), sb.toString());


        if (request1.getMethod().equals(RequestMethod.OPTIONS.toString())) {
//            response1.setStatus(HttpStatus.OK.value());
            return;
        }
//        chain.doFilter(request, response);
        //对请求进行拦截,防xss处理
        chain.doFilter(new XssHttpServletRequestWrapper(request1), response);
    }


    // 字符串读取
    // 方法一
    public String ReadAsChars(HttpServletRequest request) {

        BufferedReader br = null;
        StringBuilder sb = new StringBuilder("");
        try {
            br = request.getReader();
            String str;
            while ((str = br.readLine()) != null) {
                sb.append(str);
            }
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }


    // 方法二
    public String ReadAsChars3(HttpServletRequest request) {
        StringBuilder sb = new StringBuilder();
        InputStream inputStream = null;
        BufferedReader reader = null;
        try {
            inputStream = request.getInputStream();
            reader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8")));
            String line = "";
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();

    }

    public String ReadAsChars2(HttpServletRequest request) {
        StringBuilder sb = new StringBuilder();
        InputStream is = null;
        try {
            is = request.getInputStream();
            byte[] b = new byte[4096];
            for (int n; (n = is.read(b)) != -1; ) {
                sb.append(new String(b, 0, n));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }
}
